//:C04:Stack.cpp {O}
// Linked list with nesting
// Linked list with nesting
#include "Stack.h"
#include "../require.h"
using namespace std;

void Stack::Link::initialize(void *dat, Link *nxt)
{
    data = dat;
    next = nxt;
}

void Stack::initialize() { head = 0;}

void Stack::push(void *dat)
{
    Link *newlink = new Link;
    newlink->initialize(dat, head);
    head = newlink;
}

void *Stack::peek()
{
    require((head != 0), "Stack empty");
    return head->data;
}
void *Stack::pop()
{
    if(head == 0)
    {
        return 0;
    }

    void *result = head->data;
    Link *oldHead = head;
    head = head->next;
    delete oldHead;
    return result;
}

void Stack::cleanup()
{
    require((head == 0), "Stack not empty");
}